<template>
	<view>
		<!-- :historyForm="detailObj" -->
		<disHistory ref="changHistory" :titleName="detailObj.orderType=== 'history_not_repaired' ? '病害变更' : '修改病害'" compoType="change"
			 :beforeChange="beforeForm" @updateSubmit="updateSubmit"></disHistory>
	</view>
</template>

<script>
	import disHistory from '../../common/disHistory.vue'
	import {
		diseaseOrderSave,
		diseaseOrderGetDetails,
		getLastFillingRecord
	} from "@/api/engineeringMaintenance/engineeringMaintenance.js"
	import mixins from '@/utils/mixins.js'
	// import {doGet,doSave} from "@/utils/offline/index.js"
	import {
		getPicture
	} from '@/api/doc.js' //获取图片
	import {
		doGet,
		doSave,
		doDelete
	} from "@/utils/offline/sqlite/index.js";
	//巡检的所有接口
	import * as XUNJIAN from '@/api/engineeringMaintenance/engineeringMaintenance.js'
	import {setNetworkState, Network_Offline, Network_Online} from '@/utils/offline/config.js'
	import {getImageBase64} from "@/utils/imageOperation.js"
	export default {
		name: 'newDiseases',
		components: {
			disHistory
		},
		mixins: [mixins],
		data() {
			return {
				detailObj: {},
				beforeForm: {},
				imgs: [],
				requestStatus: false
			}
		},
		methods: {
			async updateSubmit(obj) {
				this.$set(obj, 'orderType','change')
				this.$set(obj, 'projectId', uni.getStorageSync('isOwner'))
				const {result} =  await doGet(getLastFillingRecord, JSON.parse(uni.getStorageSync('lineData')))
				this.$set(obj,'inspectUser',result.inspectUser)
				// console.log(obj, '父组件updateSubmit------之前')
				// console.log('历史未修复入参',obj);
				// return
				if(obj && obj.videoFileList && obj.videoFileList.length > 0){
					const taskArr = obj.videoFileList.filter(it => it.uploadStatus)
					if(taskArr.length > 0) {
						uni.showToast({
							title:'视频上传中,请稍等',
							icon:'none',
							mask:true
						})
						return
					}
				}
				if(this.requestStatus) return
				this.requestStatus = true
				doSave(XUNJIAN.diseaseOrderSave,{...obj,_from:'history_not_repaired'})
				.then(async res => {
					if (res.success) {
						uni.showToast({
							title: '成功',
							icon: 'none',
							mask:true,
						})
						setTimeout(() => {
							uni.navigateBack()
							this.requestStatus = false
						},1600)
					}else {
						this.requestStatus = false
					}
				})
			},
		},
		onLoad(params) {
			if (params && params.id) {
				this.mileage = params.type
				let imgs
				let videos
				// console.log(params.id, '详情id入参');
				// diseaseOrderGetDetails({id: params.id})
				doGet(XUNJIAN.diseaseOrderGetDetails, {id:params.id})
				.then(async res => {
					// console.log({...res.result},'未修复详情')
					if (!res.success) return
					this.detailObj = res.result
					//在历史未修复中将病害默认为升降级
					this.$set(this.detailObj, 'diseaseChange', 'relegation')
					if (res.result.docId) {
						const attachRes = await doGet(getPicture, {docId:res.result.docId})
						// console.log(attachRes,'----92----行');
						if(attachRes.success && attachRes.result) {
							imgs = attachRes.result.map(it => ({
								id:it.id,
								url: it.fileName
									?  this.$config.IMG_FILE_URL + it.fileName
									:  it.url 
							}))
						}
					}
					if (res.result.videoDocId) {
						const attachRes = await doGet(getPicture, {
							docId: res.result.videoDocId
						})
						if (attachRes.success && attachRes.result) {
							// let videoResult = attachRes.result.filter(item => item.contentType.substring(0,
							// 	item.contentType.indexOf("/")) === 'video')
							videos = attachRes.result.map(it => ({
								id: it.id,
								url: it.fileName ?
									this.$config.VIDEO_URL + it.fileName :
									it.url
							}))
							
						}
					}
					this.$set(this.detailObj, 'mileage', this.mileage)
					
					//变更病害前对象(用同样的绑定值修改上面会影响到下面(例如备注等字段))
					this.beforeForm = {
						startMileage: this.getContent2(this.detailObj),
						remark: this.detailObj.diseaseRemarks,
						ringNumber: `${this.detailObj.startNumber || ''}${this.detailObj.startNumber && this.detailObj.endNumber ? '-' : ''}${this.detailObj.endNumber || ''}`,
						otherPosition: this.detailObj.others,
						clockPosition: this.detailObj.clockwisePosition,
						nowStatus: this.detailObj.diseaseStatus,
						level: this.detailObj.diseaseGradeName + (this.detailObj.diseaseNo ? this.detailObj
							.diseaseNo : ''),
						imgs: imgs || [],
						measure:this.detailObj.repairMethod,
						stationLocation:this.detailObj.stationLocation,
						createDate:this.detailObj.createDate,
						createUserName:this.detailObj.createUserName,
						videos:videos || []
					}
					console.log(this.beforeForm, '自己拼接的对象');
					//病害现状需要用户自己填
					this.$set(this.detailObj, 'diseaseStatus', '')
					this.$set(this.detailObj, 'diseaseGrade', '')
					this.$set(this.detailObj, 'diseaseNo', '')
					this.$set(this.detailObj, 'diseaseStandardLibraryId', '')
					this.$set(this.detailObj, 'diseaseRemarks', '')
					this.$set(this.detailObj, 'repairMethod', '')
					//重置id是因为需要历史未修复中需要把照片重新传
					this.$set(this.detailObj, 'docId', undefined)
					this.$set(this.detailObj, 'videoDocId', undefined)
					this.$set(this.detailObj, 'videoFileList', [])
					this.detailObj.removeFileSet = new Set()
					this.$refs.changHistory.historyForm = this.detailObj
				})

			}
		}
	}
</script>

<style lang="scss" scoped>

</style>
